package com.danceforyou.customer.service.impl;

import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.service.OperationService;
import com.danceforyou.common.service.impl.BaseServiceImpl;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.customer.dao.CustomerDAO;
import com.danceforyou.customer.model.CustomerDO;
import com.danceforyou.customer.model.CustomerUserDO;
import com.danceforyou.customer.service.CustomerService;
import com.danceforyou.customer.service.CustomerUserService;
import com.danceforyou.customer.vo.CustomerVO;
import com.danceforyou.user.model.UserDO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;

/**
* 类名称：CustomerServiceImpl
* 创建人：danceforyou
* 创建时间：2021年02月03日
* 版本号：1.0v
*/
@Service("customerService")
public class CustomerServiceImpl extends BaseServiceImpl<Long, CustomerDO, CustomerVO, CustomerDAO> implements CustomerService {
    @Autowired
    private CustomerDAO customerDAO;
    @Autowired
    private OperationService operationService;
    @Autowired
    private CustomerUserService customerUserService;
    @Autowired(required = false)
    private HttpSession session;

    @Override
    public CustomerDAO getDao() {
        return customerDAO;
    }

    @Override
    public void saveInfo(CustomerDO customer) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        //客户品牌唯一验证
        CustomerDO temp = this.getByCondition(new HashMap<String, Object>(){{
            put("isDelete", false);
            put("brand", customer.getBrand());
        }});
        AssertUtil.isNull(temp, "该客户品牌已存在，请勿重复创建");
        customer.setCreateUserId(loginUser.getId());
        customer.setCreateDate(new Date());
        customer.setStatus(Constants.CUSTOMER_STATUS_ENABLE);
        customer.setIsDelete(false);
        this.insert(customer);
        //默认创建后为本人授权客户信息
        CustomerUserDO customerUser = new CustomerUserDO();
        customerUser.setCustomerId(customer.getId());
        customerUser.setBelongUserId(loginUser.getId());
        customerUser.setAuthUserId(loginUser.getId());
        customerUser.setAuthDate(customer.getCreateDate());
        customerUserService.insert(customerUser);
        //记录操作日志
        operationService.saveOperation(Constants.OPER_TABLE_CUSTOMER, customer.getId(), Constants.OPER_ADD, loginUser, null);
        operationService.saveOperation(Constants.OPER_TABLE_CUSTOMER, customer.getId(), Constants.OPER_AUTH, loginUser, "创建用户时默认授权给创建人");
    }

    @Override
    public void updateInfo(CustomerDO customer) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        CustomerDO temp = this.getById(customer.getId());
        AssertUtil.notNull(temp, "信息不存在");
        AssertUtil.isTrue(Constants.ROLE_GLB_CODE.equals(loginUser.getRole().getRoleType()) || temp.getCreateUserId().equals(loginUser.getId()), "您无权修改本客户");
        //客户品牌唯一验证
        if (!customer.getBrand().equals(temp.getBrand())) {
            temp = this.getByCondition(new HashMap<String, Object>(){{
                put("isDelete", false);
                put("brand", customer.getBrand());
            }});
            AssertUtil.isNull(temp, "该客户品牌已存在，请勿重复创建");
        }
        this.update(customer);
        //记录操作日志
        operationService.saveOperation(Constants.OPER_TABLE_CUSTOMER, customer.getId(), Constants.OPER_EDIT, loginUser, null);
    }

    @Override
    public int deleteInfoByIds(String ids) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        String[] idArr = ids.split(",");
        int i = 0;
        for (String id : idArr) {
            CustomerDO customer = this.getById(Long.valueOf(id));
            AssertUtil.notNull(customer, "信息不存在");
            AssertUtil.isTrue(Constants.ROLE_GLB_CODE.equals(loginUser.getRole().getRoleType()) || customer.getCreateUserId().equals(loginUser.getId()), "非法操作");
            i += this.setDelete(customer);
            //记录操作日志
            operationService.saveOperation(Constants.OPER_TABLE_CUSTOMER, customer.getId(), Constants.OPER_DELETE, loginUser, null);
        }
        return i;
    }

    @Override
    public void setCustomerEnable(Long customerId) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        CustomerDO customer = this.getById(customerId);
        AssertUtil.notNull(customer, "信息不存在");
        AssertUtil.isTrue(Constants.ROLE_GLB_CODE.equals(loginUser.getRole().getRoleType()) || customer.getCreateUserId().equals(loginUser.getId()), "非法操作");
        customer.setStatus(Constants.CUSTOMER_STATUS_ENABLE);
        this.update(customer);
        //记录操作日志
        operationService.saveOperation(Constants.OPER_TABLE_CUSTOMER, customer.getId(), Constants.OPER_ENABLE, loginUser, null);
    }

    @Override
    public void setCustomerDisable(Long customerId) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        CustomerDO customer = this.getById(customerId);
        AssertUtil.notNull(customer, "信息不存在");
        AssertUtil.isTrue(Constants.ROLE_GLB_CODE.equals(loginUser.getRole().getRoleType()) || customer.getCreateUserId().equals(loginUser.getId()), "非法操作");
        customer.setStatus(Constants.CUSTOMER_STATUS_DISABLE);
        this.update(customer);
        //记录操作日志
        operationService.saveOperation(Constants.OPER_TABLE_CUSTOMER, customer.getId(), Constants.OPER_DISABLE, loginUser, null);
    }
}
